<?php
namespace app\madmin\controller;
use app\madmin\model\MallGoodsModel;
use app\madmin\model\MallClassModel;

/**
 * 商品物品控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class MallGoods extends PublicBase{

    # 商品物品列表方法
    public function showList(){
        $MallGoods = new MallGoodsModel;

        $Where = $this->getFormWhere();

        # 商品分类
        $goods_class = input('param.goods_class');
        if(!empty($goods_class)){
            $Where['goods_class'] = $goods_class;
        }
        $this->assign('goods_class', $goods_class);

        $goods_state = input('param.goods_state');
        if(!empty($goods_state)){
            $Where['goods_state'] = $goods_state;
        }
        $this->assign('goods_state', $goods_state);

        $Lists = $MallGoods->mList($Where, '*', config('CNF_LISTPAGE'));

        # 模版赋值
        return $this->fetch('MallGoods/showList', [
            'Lists' => $Lists,
            'Class' => $this->showClass()
        ]);
    }

    # 商品物品新增方法
    public function showAdd(){
        $MallGoods = new MallGoodsModel;

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $MallGoods->mAdd($Param);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            # 模版赋值
            return $this->fetch('MallGoods/showAdd', [
                'Info'  => Null,
                'Class' => $this->showClass()
            ]);
        }
    }

    # 商品物品更新方法
    public function showEdit(){
        $MallGoods = new MallGoodsModel;

        $ID = input('param.id');

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $MallGoods->mEdit(['goods_id'=>$ID], $Param);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            $Info = $MallGoods->mInfo(['goods_id'=>$ID]);

            if(empty($Info)){
                $this->ShowData(2, lang('XtCwCxCw'));
            }else{
                # 模版赋值
                return $this->fetch('MallGoods/showAdd', [
                    'Info'  => $Info,
                    'Class' => $this->showClass()
                ]);
            }
        }
    }

    # 商品物品删除方法
    public function showDel(){
        if(request()->isPost()){
            $MallGoods = new MallGoodsModel;

            $ID = input('post.id');

            $Back = $MallGoods->mDel(['goods_id'=>$ID]);

            $this->ShowData($Back['code'], $Back['msgs']);
        }
    }

    # 商品分类下拉方法
    private function showClass(){
        $MallClass = new MallClassModel;

        $Where = [];
        $Where['class_state'] = 1;

        $Lists = $MallClass->mAll($Where);

        return $MallClass::mTree($Lists);
    }

}